//
//  HQStyleListView.m
//  MobileiOS
//
//  Created by 郝庆松 on 2018/5/19.
//  Copyright © 2018年 PCITC. All rights reserved.
//

#import "HQStyleListView.h"

@interface HQStyleListView()  <UICollectionViewDataSource, UICollectionViewDelegate>

@end

@implementation HQStyleListView

+ (instancetype)loadTopHotView{
    return [[[NSBundle mainBundle] loadNibNamed:@"HQStyleListView" owner:nil options:nil] lastObject];
}

- (void)awakeFromNib{
    [super awakeFromNib];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerNib:[UINib nibWithNibName:@"HQStyleListCell" bundle:nil] forCellWithReuseIdentifier:@"HQStyleListCell"];
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    _layout.minimumLineSpacing = 5;
    _layout.minimumInteritemSpacing = 5;
    _layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    _layout.itemSize = CGSizeMake((self.frame.size.width-5*2) / 3 ,50);
}

-(void)setDataSource:(NSArray *)dataSource{
    _dataSource = dataSource;
    [self.collectionView reloadData];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HQStyleListCell *cell = [HQStyleListCell cellWithCollectionView:collectionView withIndexPath:indexPath];
    cell.itemName.text = [NSString stringWithFormat:@"%@",_dataSource[indexPath.row][@"name_ext"]];
    cell.dataInfo = _dataSource[indexPath.row];
    return cell;
}

-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

-(void)selectIndexPath:(NSIndexPath *)indexPath{
    [self collectionView:_collectionView didSelectItemAtIndexPath:indexPath];
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [self.delegate selectItem:self withInfo:_dataSource[indexPath.row] withIndexPath:indexPath];
}

@end
